﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.Infra.Service.Common;
using System.Data;

namespace Sinacor.OMM.Deployment.Configurator.ResourceAccess
{
    public class Repository : BaseResourceAccess
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public Repository(Int32 companyId)
            : base(companyId)
        {

        }

        #endregion

        #region Methods

        public void AlterarRepositorio(Entity.Repository entity)
        {
            AlterarConnectionString(entity);
            AlterarMessageLimit(entity);
            AlterarBytesLimit(entity);
        }

        public void AlterarConnectionString(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"UPDATE TBOREPRIO_DET SET
                                  TXT_CNTD_PRDD  = UPPER(:TXT_CNTD_PRDD)
                            WHERE NOME_PRDD = 'CONNECTION_STRING' AND COD_REPRIO = :COD_REPRIO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TXT_CNTD_PRDD", DbType.String, entity.Fila);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);
                
                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public void AlterarMessageLimit(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"UPDATE TBOREPRIO_DET SET
                                  TXT_CNTD_PRDD  = UPPER(:TXT_CNTD_PRDD)
                            WHERE NOME_PRDD = 'QUEUE_MESSAGE_LIMIT' AND COD_REPRIO = :COD_REPRIO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TXT_CNTD_PRDD", DbType.Int32, entity.LimiteMensagem);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public void AlterarBytesLimit(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"UPDATE TBOREPRIO_DET SET
                                  TXT_CNTD_PRDD  = UPPER(:TXT_CNTD_PRDD)
                            WHERE NOME_PRDD = 'QUEUE_BYTES_LIMIT' AND COD_REPRIO = :COD_REPRIO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TXT_CNTD_PRDD", DbType.Int32, entity.LimiteTamanho);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        public Entity.Repository IncluirRepository(Entity.Repository entity)
        {
            String sql = String.Empty;
            String sqlCommand = String.Empty;

            sql = @"SELECT max(NVL(COD_REPRIO,0)) + 1 FROM TBOREPRIO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql))
            {
                entity.Codigo = Convert.ToInt32(this.DataBase.ExecuteScalar(command));
            }

            sqlCommand = @"INSERT INTO TBOREPRIO a
                           (a.COD_REPRIO,a.NOME_REPRIO ,a.DESC_REPRIO ,a.COD_TIPO_REPRIO)
                            VALUES
                            (:COD_REPRIO, 
                            :NOME_REPRIO,
                            :DESC_REPRIO,
                            :COD_TIPO_REPRIO)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);
                this.DataBase.AddInParameter(command, "NOME_REPRIO", DbType.String, "REPOSITÓRIO DO HFT PROCESS " + entity.Codigo);
                this.DataBase.AddInParameter(command, "DESC_REPRIO", DbType.String, "Repositório das mensagens que serão processadas pelo processo HFT 1");
                this.DataBase.AddInParameter(command, "COD_TIPO_REPRIO", DbType.Int32, 1);
                
                this.DataBase.ExecuteNonQuery(command);
            }

            IncluirConnectionString(entity);
            IncluirMessageLimit(entity);
            IncluirBytesLimit(entity);

            return entity;
        }

        private void IncluirConnectionString(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"INSERT INTO TBOREPRIO_DET a
                           (a.COD_REPRIO, a.NOME_PRDD ,a.TXT_CNTD_PRDD)
                            VALUES
                            (:COD_REPRIO, 
                            'CONNECTION_STRING',
                            :TXT_CNTD_PRDD)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TXT_CNTD_PRDD", DbType.String, entity.Fila);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        private void IncluirMessageLimit(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"INSERT INTO TBOREPRIO_DET a
                           (a.COD_REPRIO, a.NOME_PRDD ,a.TXT_CNTD_PRDD)
                            VALUES
                            (:COD_REPRIO, 
                            'QUEUE_MESSAGE_LIMIT',
                            :TXT_CNTD_PRDD)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TXT_CNTD_PRDD", DbType.String, entity.LimiteMensagem);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        private void IncluirBytesLimit(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"INSERT INTO TBOREPRIO_DET a
                           (a.COD_REPRIO, a.NOME_PRDD ,a.TXT_CNTD_PRDD)
                            VALUES
                            (:COD_REPRIO, 
                            'QUEUE_BYTES_LIMIT',
                            :TXT_CNTD_PRDD)";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TXT_CNTD_PRDD", DbType.String, entity.LimiteTamanho);
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }

        }

        public void ExcluirRepository(Entity.Repository entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"DELETE FROM TBOREPRIO_DET a
                           WHERE COD_REPRIO = :COD_REPRIO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);
                
                this.DataBase.ExecuteNonQuery(command);
            }

            sqlCommand = @"DELETE FROM TBOREPRIO a
                           WHERE COD_REPRIO = :COD_REPRIO";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_REPRIO", DbType.Int32, entity.Codigo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        #endregion
    }
}
